package api

import (
	"context"
	"errors"
	"fmt"
	"github.com/labstack/echo/v4"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	"longmen/server/pkg/protobuf"
)

// @Summary 单个红包最低限额
// @Description 单个红包最低限额
// @Accept  json
// @Produce  json
// @Tags hongbao
// @Success 200 {object} int "result"
// @Router /api/hongBao/getHongBaoSetting [POST]
func (base *Controller) GetHongBaoSetting(c echo.Context) error {
	hongLowPrice := global.Config.GetFloat64("hongbao.hongLowPrice")
	if hongLowPrice <= 0.0 {
		hongLowPrice = _const.HONGBAO_LOW_PRICE
	}
	return util.BuildSuccess(c, hongLowPrice, _const.HTTP_RESP_MSG_SUC)
}

/**
生产一个红包
*/

// @Summary 生成一个红包
// @Description 生成一个红包
// @Accept  json
// @Produce  json
// @Tags hongbao
// @Param   sendBody body protobuf.ProvideHongBaoReq true "body"
// @Success 200 {object} protobuf.ProvideHongBaoResp "result"
// @Router /api/hongBao/provideHongBao [POST]
func (base *Controller) ProvideHongBao(c echo.Context) error {
	//参数检测
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}
	req := &protobuf.ProvideHongBaoReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), &req); err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	//红包最低金额
	hongLowPrice := global.Config.GetFloat64("hongbao.hongLowPrice")
	if hongLowPrice <= 0.0 {
		hongLowPrice = _const.HONGBAO_LOW_PRICE
	}

	req.UserId = int64(uid)
	if req.UserId <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("总红包克拉不能小于等于零")))
	}
	//参数检测
	if req.TotalDiamond <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("总红包克拉不能小于等于零")))
	}

	if req.TotalQuantity <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("发送红包数量不能小于等于零")))
	}

	if req.TotalDiamond/req.TotalQuantity < _const.HONGBAO_LOW_PRICE {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("发送红包单个平均不能低于%d", _const.HONGBAO_LOW_PRICE)))
	}

	if req.DelayTime <= 0 {
		req.DelayTime = _const.DEFALUT_DELAYTIME
	}
	//验证usrid,是否为自已
	bao, err := grpc_client.HongBaoServerClient.ProvideHongBao(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, "发送红包失败", err)
	}

	return util.BuildSuccess(c, bao, _const.HTTP_RESP_MSG_SUC)
}

// 抢红包
// @Summary 抢红包
// @Description 抢红包
// @Accept  json
// @Produce  json
// @Tags hongbao
// @Param   sendBody body protobuf.GrabHongBaoReq true "body"
// @Success 200 {object} protobuf.GrabHongBaoResp "result"
// @Router /api/hongBao/grabHongBao [POST]
func (base *Controller) GrabHongBao(c echo.Context) error {
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}
	req := &protobuf.GrabHongBaoReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), &req); err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	req.UserId = int64(uid)
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	bao, err := grpc_client.HongBaoServerClient.GrabHongBao(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, "操作失败", err)
	}
	return util.BuildSuccess(c, bao, _const.HTTP_RESP_MSG_SUC)
}

// 抢红包
// @Summary 查询红包列表
// @Description 查询红包列表
// @Accept  json
// @Produce  json
// @Tags hongbao
// @Param   sendBody body protobuf.GetHongBaoListReq true "body"
// @Success 200 {object} protobuf.ItemHongBaoReq "result"
// @Router /api/hongBao/hongBaoList [POST]
func (base *Controller) HongBaoList(c echo.Context) error {
	req := &protobuf.GetHongBaoListReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), &req); err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	list, err := grpc_client.HongBaoServerClient.GetHongBaoList(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, "查询红包信息出错", err)
	}
	return util.BuildSuccess(c, list, _const.HTTP_RESP_MSG_SUC)
}
